package a.b.reggie.service.impl;

import a.b.reggie.dto.SetmealDto;
import a.b.reggie.mapper.SetmealMapper;
import a.b.reggie.pojo.Dish;
import a.b.reggie.pojo.Setmeal;
import a.b.reggie.pojo.SetmealDish;
import a.b.reggie.service.IDishService;
import a.b.reggie.service.ISetmealDishService;
import a.b.reggie.service.ISetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class ISetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private IDishService dishService;


    /**
     * 添加套餐，以及与菜品的映射
     * @param setmealDto
     */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        //添加套餐的基本信息
        this.save(setmealDto);
        //获取套餐的id
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Setmeal::getName,setmealDto.getName());
        Setmeal setmeal = this.getOne(queryWrapper);
        Long setmealId = setmeal.getId();
        //获取菜品与套餐映射关系的对象
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        //添加菜品与套餐映射关系的对象
        for(SetmealDish setmealDish:setmealDishList){
            setmealDish.setSetmealId(setmealId);
            setmealDishService.save(setmealDish);
        }
    }

    /**
     * 删除套餐，以及与菜品的映射
     * @param list
     */
    @Transactional
    public void removeWithDish(List<Long> list) {
        //删除套餐基本信息
        this.removeByIds(list);
        //删除套餐与菜品的映射
        for (Long id:list){
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(queryWrapper);
        }
    }

    /**
     * 修改套餐，以及与菜品的映射
     * @param setmealDto
     */
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐基本信息
        this.updateById(setmealDto);
        //删除对应的菜品信息
        Long setmealDtoId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDtoId);
        setmealDishService.remove(queryWrapper);
        //添加对应的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(setmealDtoId);
            setmealDishService.save(setmealDish);
        }
    }

    /**
     * 更改售卖状态
     * @param num
     * @param ids
     */
    public void status(int num, Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        //获取套餐集合
        List<Setmeal> setmeals = this.listByIds(list);
        //修改售卖状态
        for(Setmeal setmeal:setmeals){
            setmeal.setStatus(num);
        }
        //修改套餐集合
        this.updateBatchById(setmeals);
    }

    /**
     * 根据分类查询套餐集合
     * @param categoryId
     * @param status
     * @return
     */
    public List<Setmeal> getListByCategoryId(Long categoryId, int status) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(Setmeal::getStatus,status);

        List<Setmeal> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 根据套餐id或食品id查询
     * @param id
     * @return
     */
    public Object getDish(Long id) {
        //先查询套餐
        Setmeal byId = this.getById(id);
        if (byId!=null){
            //查到套餐就返回套餐
            return byId;
        }else {
            Dish dish = dishService.getById(id);
            return dish;
        }
    }

}
